package com.redroid.findme;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHandler extends SQLiteOpenHelper{

	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "findme";
	private static final String TABLE_ACCOUNTS = "accounts";
	
	private static final String KEY_ID = "id";
	private static final String KEY_USERNAME = "username";
	private static final String KEY_PASSWORD = "password";
	private static final String KEY_SOCIALNETWORK = "socialnetwork";
	
	public DbHandler(Context context){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_ACCOUNTS_TABLE = "CREATE TABLE " + TABLE_ACCOUNTS 
		+ "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_USERNAME +
		" TEXT, " + KEY_PASSWORD + " TEXT, " +
		KEY_SOCIALNETWORK + " TEXT)";
		db.execSQL(CREATE_ACCOUNTS_TABLE);
		
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		db.execSQL("DROP TABLES IF EXISTS " + TABLE_ACCOUNTS);
		onCreate(db);
	}

	/*
	 * CRUD stuff
	 */
	
	public void addAccount(Contact contact){
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put(KEY_USERNAME, contact.getUsername());
		values.put(KEY_PASSWORD, contact.getPassword());
		values.put(KEY_SOCIALNETWORK, contact.getSN());
		
		db.insert(TABLE_ACCOUNTS, null, values);
		
		db.close();
	}

	public Contact getContact(int id){
		
		SQLiteDatabase db = this.getReadableDatabase();
		
		Cursor cursor = db.query(TABLE_ACCOUNTS, new String[] {KEY_ID,
				KEY_USERNAME, KEY_PASSWORD, KEY_SOCIALNETWORK},
				KEY_ID + "=?", new String[] {String.valueOf(id)}, null, null
				, null, null);
		if(cursor != null){
			cursor.moveToFirst();
		}
	
		Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), cursor.getString(2), cursor.getString(3));
		cursor.close();
		db.close();
		return contact;
	}
	
	public List<Contact> getAllContacts(){
		
		List<Contact> contactList = new ArrayList<Contact>();

		String selectQuery = "SELECT * FROM " + TABLE_ACCOUNTS;
		
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		
		if(cursor.moveToFirst()){
		do {
			Contact contact = new Contact();
			contact.setID(Integer.parseInt(cursor.getString(0)));
			contact.setUsername(cursor.getString(1));
			contact.setPassword(cursor.getString(2));
			contact.setSN(cursor.getString(3));
			contactList.add(contact);
			} while (cursor.moveToNext());	
		}
		db.close();
		return contactList;
	} 
	
	public int getContactCount(){
		String countQuery = "SELECT * FROM " + TABLE_ACCOUNTS;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		db.close();
		return cursor.getCount();
	}
	
	public int updateContact(Contact contact){
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put(KEY_USERNAME, contact.getUsername());
		values.put(KEY_PASSWORD, contact.getPassword());
		values.put(KEY_SOCIALNETWORK, contact.getSN());
		
		return db.update(TABLE_ACCOUNTS, values, KEY_ID + " = ?", 
				new String[] {String.valueOf(contact.getID())});
		
		
	}
	
	public void deleteContact(Contact contact){
		SQLiteDatabase db = this.getWritableDatabase();
		
		db.delete(TABLE_ACCOUNTS, KEY_ID + " = ?", 
				new String[] {String.valueOf(contact.getID())});
		
		db.close();
	}
	
}
